#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Motor.h"
#include "Key.h"
#include "PWM.h"
void oled_gui(void);
void CCT_set(uint16_t *cct);
void power_set(uint16_t *power);
void key_scan(void);

uint8_t MODE = 0;
uint16_t power = 0;    
uint16_t led_power_N , led_power_L;     //定义速度变量
uint16_t Speed = 0;     
uint16_t cct = 4500;

int main(void)
{
    /*模块初始化*/
    OLED_Init();        //OLED初始化
    PWM_Init();
    Motor_Init();       //直流电机初始化
    Key_Init();         //按键初始化
    /*显示静态字符串*/
    OLED_ShowString(1, 7, "CCT  ");
    Delay_ms(100);
    while (1)
    {
        key_scan();
        MotorB_SetSpeed((Speed + 300));
        if(MODE == 0)
        {
            CCT_set(&cct);
        }
        if(MODE == 1)
        {
            power_set(&power);
        }
        oled_gui();
    }
}


void oled_gui(void)
{
    OLED_ShowString(1, 1, "MODE:");
    OLED_ShowString(2, 1, "CCT:"); OLED_ShowNum(2, 7, cct, 4); OLED_ShowString(2, 12, "K");
    OLED_ShowString(3, 1, "Light:");OLED_ShowNum(3, 7, power, 3);OLED_ShowString(3, 12, "%");
    OLED_ShowString(4, 1, "FAN:");OLED_ShowNum(4, 7, Speed, 3);OLED_ShowString(4, 12, "%");

}

void CCT_set(uint16_t *cct)
{
    if(*cct > 6000)
    {
        *cct = 6000;
    }
    if(*cct < 3000)
    {
        *cct = 3000;
    }
    led_power_N = -(0.05*(*cct)) + 300;
    led_power_L = 0.1*(*cct) - 300;
    PWM_SetCompare_led1(led_power_N);
    PWM_SetCompare_led2(led_power_L);
}


void power_set(uint16_t *power)
{
    uint16_t led_power;
    if(*power < 5)
    {
        *power = 0;
    }
    if(*power > 100)
    {
        *power = 100;
    }
    led_power = *power * 2;
    PWM_SetCompare_led1(led_power);
    PWM_SetCompare_led2(led_power);
}

void key_scan(void)
{
    static uint8_t i = 1;
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0)
    {
        Delay_ms(20);                                           //延时消抖
        while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0);  //等待按键松手
        Delay_ms(20);                                           //延时消抖
        OLED_ShowString(i, 14, "  ");
        i--;
    }

    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 0)          //读PB1输入寄存器的状态，如果为0，则代表按键1按下
    {
        Delay_ms(20);                                           //延时消抖
        while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 0);  //等待按键松手
        Delay_ms(20);                                           
        OLED_ShowString(i, 14, "  ");
        i++;
    }

    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 0)         
    {
        Delay_ms(20);                                           
        while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 0);  
        Delay_ms(20);                                           
        if(i == 1)
        {
            MODE = 1;
            OLED_ShowString(1, 7, "Light");
        }
        else if(i == 2 && MODE == 0)
        {
            cct += 100;
        }
        else if(i == 3 && MODE == 1)
        {
            power += 5;
        }
        else if(i == 4)
        {
            Speed += 10;
        }
    }

    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 0)         
    {
        Delay_ms(20);                                           
        while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 0);  
        Delay_ms(20);                                           
        if(i == 1)
        {
            MODE = 0;
            OLED_ShowString(1, 7, "CCT  ");
        }
        else if(i == 2 && MODE == 0)
        {
            cct -= 100;    
        }
        else if(i == 3 && MODE == 1)
        {
            power -= 5;
        }
        else if(i == 4)
        {
            Speed -= 10;
        }
    }
    
    if(i <= 4)
    {
        OLED_ShowString(i, 14, "<-");
    }
    if(i <= 0 ) { i = 1; }
    if(i >= 5 ) { i = 4; }

}


